package com.tusuo.stds.ui.suggest;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.ImageView;

import com.tusuo.stds.R;
import com.tusuo.stds.rest.MyHashMap;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import rx.functions.Action0;

/**
 * Created by developer on 17-5-2.
 */

public class PictureSelectActivity extends BaseToolbarActivity<EmptyView,EmptyPresenter> {

    GridView gvPic;

    final String[] colums = {MediaStore.Images.Media.DATA,MediaStore.Images.Media._ID};
    final String orderBy = MediaStore.Images.Media.DATE_TAKEN;

    ArrayList<String> imageUrls;
    HashMap<Integer,String> checkedUrls;

    int checkedCount = 0;
    private List<ToolbarOwner.MenuAction> menuActionList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture_select);
        gvPic = (GridView) findViewById(R.id.pic_select_gv);
        initData();
        gvPic.setAdapter(new MyAdapter());


    }

    @NonNull
    @Override
    public EmptyPresenter createPresenter() {
        return new EmptyPresenter();
    }

    private void initData() {
        setupUI(this,getResources().getColor(R.color.white),getResources().getColor(R.color.black));
        imageUrls = new ArrayList<>();
        checkedUrls = new HashMap<>();
        Cursor cursor = getApplication()
                .getContentResolver()
                .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, colums, null, null, null);
        for (int i = 0;i < cursor.getCount();i ++) {
            cursor.moveToPosition(i);
            String cursorStr = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            imageUrls.add(cursorStr);
        }

        menuActionList = new ArrayList<>();

        ToolbarOwner.MenuAction menuAction = new ToolbarOwner.MenuAction("完成", new Action0() {
            @Override
            public void call() {
                MyHashMap myMap = new MyHashMap(checkedUrls);
                Intent intent = new Intent();
                intent.putExtra("imageUrls",myMap);
                setResult(10023,intent);
                finish();
            }
        }, 0);

        menuActionList.add(menuAction);

        setConfig(concatConfig(checkedCount),menuActionList);
    }

    public String concatConfig(int index) {
        String config = "已选择" + index + "张图片";
        return config;
    }

    public void setConfig(String configStr, List<ToolbarOwner.MenuAction> menuActionList) {
        ToolbarOwner.Config config = new ToolbarOwner.Config(configStr, true, R.drawable.ic_back_dark, menuActionList);
        mToolbarOwner.setConfig(config);
    }


    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            if (imageUrls.size() == 0 && imageUrls == null) {
                return 0;
            }else {
                return imageUrls.size();
            }
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view;
            final ViewHolder holder;

            if (convertView == null) {
                view = View.inflate(PictureSelectActivity.this,R.layout.pic_select_list_item,null);
                holder = new ViewHolder();
                holder.imageView = (ImageView) view.findViewById(R.id.pic_list_item_iv);
                holder.checkBox = (CheckBox) view.findViewById(R.id.pic_list_item_cb);
                view.setTag(holder);
            }else {
                view = convertView;
                holder = (ViewHolder) view.getTag();
            }


            String path = "file://" + imageUrls.get(position);

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 2;

            Bitmap bitmap = BitmapFactory.decodeFile(imageUrls.get(position),options);

            holder.imageView.setImageBitmap(bitmap);

            /*Glide.with(PictureSelectActivity.this)
                    .load(uri)
                    .centerCrop()
                    .crossFade()
                    .into(holder.imageView);*/

            holder.checkBox.setTag(position);

            holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int tag = (int) buttonView.getTag();

                    if (isChecked) {
                        checkedUrls.put(tag,imageUrls.get(tag));
                        checkedCount ++;
                    }else {
                        checkedUrls.remove(tag);
                        checkedCount --;
                    }

                    setConfig(concatConfig(checkedCount),menuActionList);
                }
            });

            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (holder.checkBox.isChecked()) {
                        holder.checkBox.setChecked(false);
                    }else {
                        holder.checkBox.setChecked(true);
                    }
                }
            });

            return view;
        }
    }

    private class ViewHolder {
        ImageView imageView;
        CheckBox checkBox;
    }

}
